package com.hellogood.work.evaluate.client.service;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.work.evaluate.shared.po.Service;
import com.hellogood.work.evaluate.shared.services.ServiceService;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.toolbar.ToolStrip;

public class ServiceTab extends Tab {
	private ListGrid listGrid = null;
	private IButton modifyBtn = null;

	public ServiceTab() {
		initView();
	}

	public void initData() {
		ServiceService.Util.getInstance().getList(new ListAsyncCallback());
	}

	private void initView() {
		TextItem textItem = new TextItem("searchItem", "关键字");
		textItem.setTitleColSpan(2);
		IButton searchBtn = new IButton("查询");
		DynamicForm form = new DynamicForm();
		form.setWidth("200");
		form.setNumCols(4);
		form.setFields(textItem);
		IButton refreshBtn = new IButton("刷新");
		ToolStrip toolStrip1 = new ToolStrip();
		toolStrip1.setMembersMargin(20);
		toolStrip1.addMember(form);
		toolStrip1.addMember(searchBtn);
		toolStrip1.addMember(refreshBtn);

		ListGridField codeField = new ListGridField("servicecode", "服务编码");
		ListGridField nameField = new ListGridField("servicename", "服务名称");
		ListGridField scoreField = new ListGridField("score", "服务分值");

		listGrid = new ListGrid();
		listGrid.setShowRowNumbers(true);
		listGrid.setFields(codeField, nameField, scoreField);

		IButton addBtn = new IButton("新增");
		modifyBtn = new IButton("修改");
		modifyBtn.setDisabled(true);
		ToolStrip toolStrip2 = new ToolStrip();
		toolStrip2.setMembersMargin(20);
		toolStrip2.setMembers(addBtn, modifyBtn);

		VLayout layout = new VLayout();
		layout.setMembersMargin(5);
		layout.addMember(toolStrip1);
		layout.addMember(listGrid);
		layout.addMember(toolStrip2);
		setPane(layout);
		setTitle("服务列表");

		listGrid.addSelectionChangedHandler(new SelectionChangedHandler() {

			@Override
			public void onSelectionChanged(SelectionEvent event) {
				if (listGrid.getSelectedRecord() != null) {
					modifyBtn.setDisabled(false);
				} else {
					modifyBtn.setDisabled(true);
				}
			}
		});

		refreshBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				initData();
			}
		});

		addBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				ModifyWindow window = new ModifyWindow(ServiceTab.this, null, null, null, false);
				window.show();
				window.setSize("300", "200");
				window.centerInPage();
			}
		});

		modifyBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				Record r = listGrid.getSelectedRecord();
				if (r != null) {
					ModifyWindow window = new ModifyWindow(ServiceTab.this, r.getAttribute("servicecode"), r.getAttribute("servicename"), r.getAttribute("score"), true);
					window.show();
					window.setSize("300", "200");
					window.centerInPage();
				}
			}
		});
	}

	private class ListAsyncCallback implements AsyncCallback<List<Service>> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(List<Service> result) {
			if (result != null) {
				Record[] records = new Record[result.size()];
				for (int i = 0; i < result.size(); i++) {
					Record r = new Record();
					r.setAttribute("servicecode", result.get(i).getServiceCode());
					r.setAttribute("servicename", result.get(i).getServiceName());
					r.setAttribute("score", result.get(i).getScore());
					records[i] = r;
				}
				listGrid.setData(records);
			}
		}
	}
}
